<template>
  <div class="Users">
    <el-card>
      <!-- 用户名搜索区域 -->
    <CardHeader>
      <template #left>
        <el-form ref="form" :model="form">
          <el-form-item>
            <el-input v-model="form.username" placeholder="根据用户名搜索"></el-input>
            <el-button @click="onEmpty">清空</el-button>
            <el-button type="primary" @click="onSearch">搜索</el-button>
          </el-form-item>
        </el-form>
      </template>
      <template #right>
        <div>
          <el-button type="success" icon="el-icon-edit" @click="addUser">新增用户</el-button>
        </div>
      </template>
    </CardHeader>
    <!-- 总共 条记录 -->
    <div class="userNum">
      <el-alert
        :title="`共 ${total} 条记录`"
        type="info"
        show-icon
        :closable="false"
        >
      </el-alert>
    </div>

    <!-- 用户信息 table表格 -->
      <el-table
        :data="tableData"
        v-loading="loading"
        style="width: 100%">
        <el-table-column
          type="index"
          label="序号"
          >
        </el-table-column>
        <el-table-column
          prop="email"
          label="邮箱"
          >
        </el-table-column>
        <el-table-column
          prop="phone"
          label="联系电话"
          >
        </el-table-column>
        <el-table-column
          prop="username"
          label="用户名"
          >
        </el-table-column>
        <el-table-column
          prop="permission_group_title"
          label="权限名称"
          >
        </el-table-column>
        <el-table-column
          prop="role"
          label="角色"
          >
        </el-table-column>
        <el-table-column label="操作">
          <template slot-scope="{ row }">
            <!-- 编辑用户 -->
            <el-button
              size="mini"
              icon="el-icon-edit"
              type="primary"
              circle
              @click="onEdit(row)"
              />
              <!-- 删除用户 -->
            <el-button
              size="mini"
              type="danger"
              icon="el-icon-delete"
              circle
              @click="onDel(row)"
              v-if="row.id!==2"
              />
          </template>

        </el-table-column>
      </el-table>

      <!-- 分页区域 -->
      <pagination
        :counts="total"
        :page="({currentPage: 1,pageSize: 10})"
        @changePageSize="changePageSize"
        @changeCurrentPage="changeCurrentPage"
      />
      <!-- <div class="block">
        <el-pagination
          background
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="currentPage4"
          :page-sizes="[10, 15, 20,]"
          :page-size="10"
          layout="total, sizes, prev, pager, next, jumper"
          :total="10">
        </el-pagination>
      </div> -->
    </el-card>

    <!-- 新增用户 弹层 -->
    <UserAdd
    @handleCloseModal="loadCreateAPI"
    ref="addShowUser"
    :text="text"
    pageTitle="用户"
    @newDataes="loadCreateAPI"
    />
  </div>
</template>

<script>
import { list, remove } from '@/api/base/users.js'
import UserAdd from '../components/user-add'
export default {
  components: {
    UserAdd
  },
  data () {
    return {
      form: {
        page: 1,
        pagesize: 10,
        username: ''
      },
      total: 0,
      tableData: [],
      // currentPage4: 4,
      text: '',
      loading: false
    }
  },

  created () {
    this.loadCreateAPI()
  },

  methods: {
    // 清空输入框
    onEmpty () {
      this.form.username = ''
      this.loadCreateAPI()
    },
    // 搜索
    onSearch () {
      this.loadCreateAPI()
    },
    // 点击新增用户
    addUser () {
      this.text = '创建'
      // console.log(123)
      this.$refs.addShowUser.dialogFormV()
    },
    // 加载用户列表
    async loadCreateAPI () {
      this.loading = true
      const { data } = await list(this.form)
      this.loading = false
      console.log(data)
      this.tableData = data.list
      this.total = data.counts
    },
    // 点击编辑用户
    onEdit (row) {
      this.text = '编辑'
      this.$refs.addShowUser.dialogFormV(row)
    },
    // 点击删除用户
    onDel (row) {
      this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async () => {
        await remove({ id: row.id })
        this.$message({
          type: 'success',
          message: '删除成功!'
        })
        this.loadCreateAPI()
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
      // this.tableData.splice(index, 1)
    },
    // 分页
    changePageSize (val) {
      this.form.pagesize = val
      this.loadCreateAPI()
    },
    changeCurrentPage (val) {
      this.form.page = val
      this.loadCreateAPI()
    }
  }
}
</script>

<style scoped lang='less'>
.Users {
  .el-form  {
    display: flex;

    .el-input {
      width: 200px;
      height: 32px;
    }
    .el-button {
      margin-left: 10px;
    }
  }

  .userNum {
    margin: 20px 0;
    // background-color: #ccc;
  }

}
</style>
